#include "Sound.h"

Sound::Sound()
	: system(NULL)
	, SoundStreaming(false)
{}

bool Sound::Init()
{
	FMOD::System_Create(&system);			// create the main sound engine
	if(NULL == system)
		return false;

	// BEGIN FMOD::system setup
	system->getVersion(&version);		
	system->getNumDrivers(&numdrivers);

	if(numdrivers == 0)	
		system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);
	else
	{
		system->getDriverCaps(0, &caps, 0, 0, &speakermode);
		system->setSpeakerMode(speakermode);
		system->getDriverInfo(0, name, 256, 0);
		result = system->init(100, FMOD_INIT_NORMAL, 0);
		if(caps & FMOD_CAPS_HARDWARE_EMULATED)	
		{system->setDSPBufferSize(1024, 10);}
		if(strstr(name, "SigmaTel"))
		{system->setSoftwareFormat(48000, 
		FMOD_SOUND_FORMAT_PCMFLOAT, 0, 0, FMOD_DSP_RESAMPLER_LINEAR);}
		if(result = FMOD_ERR_OUTPUT_CREATEBUFFER)
		{system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
		system->init(100, FMOD_INIT_NORMAL, 0);}
	}

	//END FMOD::system setup
	return true;
}

bool Sound::Init(Vector<char*> effects, Vector<char*> streams)
{
	if(!Init())
		return false;

	for(int i = 0; i < effects.sizeOf(); ++i)
		LoadEffect(effects[i]);
	for(int i = 0; i < streams.sizeOf(); ++i)
		LoadStream(streams[i]);

	return true;
}

void Sound::LoadEffect(char* filename)
{
	FMOD::Sound* temp;
	system->createSound(filename, FMOD_DEFAULT, 0, &temp);
	vEffects.push(temp);
}

void Sound::LoadStream(char* filename)
{
	FMOD::Sound* temp;
	system->createStream(filename, FMOD_LOOP_NORMAL|  FMOD_2D | FMOD_HARDWARE,
							0, &temp);

	vStreams.push(temp);
}

void Sound::PlayEffect(int index)
{
	system->playSound(FMOD_CHANNEL_FREE, vEffects[index], false, 0);
}

void Sound::Stream(int index)
{
	if(!SoundStreaming)
	{
		system->playSound	
			(FMOD_CHANNEL_FREE, vStreams[index], true, & myChannel);
		myChannel->setVolume(2.0f);		// Set the volume while it is paused.
		myChannel->setPaused(false);	// This is where the sound really starts.
		SoundStreaming = true;
	}
}

void Sound::DoWork()
{
	system->update();
}

Sound::~Sound()
{
	system->release();
}





























//
//Sound::Sound()
//	: m_pEngine(NULL)
//	, m_pSoundBank(NULL)
//	, m_pEffectBank(NULL)
//	, m_pStreamingBank(NULL)
//{}
//
//
//bool Sound::Init()
//{
//	XACT3CreateEngine(0, &m_pEngine);				// create the engine
//	if(!m_pEngine)									// if creation failed, return false
//		return false;
//
//	XACT_RUNTIME_PARAMETERS RuntimeParams = {0};	// populate the parameters struct with null values
//	m_pEngine->Initialize(&RuntimeParams);			// initialize engine
//
//	// allocate memory for banks
//	m_pSoundBank = (IXACT3SoundBank*)CoTaskMemAlloc(sizeof(IXACT3SoundBank));
//
//	// create banks using the main engine
//	m_pEngine->CreateSoundBank(m_pSoundBank
//
//	// not sure if this setup is needed
//	//RuntimeParams.lookAheadTime = XACT_ENGINE_LOOKAHEAD_DEFAULT;
//	//RuntimeParams.pGlobalSettingsBuffer = CoTaskMemAlloc(XACT_FLAG_ENGINE_CREATE_MANAGEDATA);
//	//RuntimeParams.globalSettingsBufferSize = sizeof(RuntimeParams.pGlobalSettingsBuffer);
//	//RuntimeParams.globalSettingsFlags = 0;
//
//
//	return true;
//}
//
//void Sound::Update()
//{
//	m_pEngine->DoWork();
//}
//
//void Sound::Cleanup()
//{
//	m_pEngine->ShutDown();
//	m_pEngine->Release();
//	m_pSoundBank->Destroy();
//	m_pEffectBank->Destroy();
//	m_pStreamingBank->Destroy();
//}